<template>
  <div class='container'>
    <el-card style="margin: 10px">
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" :inline="true" size="mini">
            <el-form-item label="学科名称" >
              <el-input style="200px" v-model="requestParams.subjectName"></el-input>
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="clearList">清除</el-button>
              <el-button type="primary" @click="getList">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>

        <el-col :span="6" style="text-align:right">
          <el-button icon="el-icon-edit" type="success" size="mini" @click="openDialog()">新增学科</el-button>
        </el-col>
      </el-row>

      <subjects-add ref="subjectsadd" :currSubject="currSubject" @updateList='getList()'></subjects-add>

      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 10px">
      </el-alert>

      <el-table :data="subject" style="width: 100%" >
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="学科名称" prop="subjectName"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期" width="160px">
          <template slot-scope="scope">{{scope.row.addDate|parseTimeByString}}</template>
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template slot-scope="scope">{{scope.row.isFrontDisplay===1?'是':'否'}}</template>
        </el-table-column>
        <el-table-column label="二级目录" prop="twoLevelDirectory"></el-table-column>
        <el-table-column label="标签" prop="tags"></el-table-column>
        <el-table-column label="题目数量" prop="totals"></el-table-column>
        <el-table-column label="操作" width="240px">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="$router.push(`directorys?id=${scope.row.id}&name=${encodeURIComponent(scope.row.subjectName)}`)">学科分类</el-button>
          <el-button type="text" size="small" @click="$router.push(`tags?id=${scope.row.id}&name=${encodeURIComponent(scope.row.subjectName)}`)">学科标签</el-button>
          <el-button type="text" size="small" @click="openDialog(scope.row)">修改</el-button>
          <el-button type="text" size="small" @click="openDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      </el-table>

      <el-pagination  
        style="margin-top:20px;text-align:right"
        class="paginationBox"
        background
        @size-change="handleSizeChange"
         @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="requestParams.pagesize"
        layout="prev, pager, next, sizes, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { list as getSubjectsList }from '@/api/hmmm/subjects'
import { remove }from '@/api/hmmm/subjects'
import subjectsAdd from '../components/subjects-add.vue'

export default {
  components: { subjectsAdd },
  name: 'SubjectsList',
  data() {
    return {
      subject:[],
      total:0,
      currSubject: {},
      requestParams: {
        subjectName: '',
        page: 1,
        pagesize: 10,
      },
    }
  },
  created() {
    this.getList()  
  },
  methods: {
    async getList () {
      const{data} = await getSubjectsList(this.requestParams)
      console.log(data,data.counts);
      this.subject = data.items
      this.total =data.counts 
    },
    clearList() {
      this.requestParams.subjectName = ''
      this.getList()
    },
    handleSizeChange(newNum) {
        console.log(`每页 ${newNum} 条`);
        this.requestParams.page = 1
        this.requestParams.pagesize = newNum
        this.getList()
    },
    handleCurrentChange(newPage) {
      console.log(`当前页: ${newPage}`);
      this.requestParams.page = newPage
      this.getList()
    },
    openDialog (currSubject) {
      this.currSubject = currSubject || {}
      this.$nextTick(() => {
        this.$refs.subjectsadd.open()
      })
    },
    openDelete(currSubject) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.removeoneList(currSubject)
          this.$message.success('删除成功!');
          this.getList()
        }).catch(() => {
          this.$message.info('已取消删除');
              
        });
    },
    async removeoneList(currSubject) {
      const {data} = await remove(currSubject)
      console.log(data);
    }
  }
}
</script>

<style scoped lang='less'>
</style>